package com.fast.admin.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.fast.admin.common.utils.CipherUtil;
import com.fast.admin.entity.sys.User;
import com.fast.admin.entity.sys.UserRole;
import com.fast.admin.mapper.sys.IUserMapper;
import com.fast.admin.service.sys.IUserRoleService;
import com.fast.admin.service.sys.IUserService;
import com.fast.common.annotation.Commit;
import com.fast.common.base.service.impl.BaseService;
import com.fast.common.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title：UserService
 * @author：ylc
 * @date：2018/8/1 11:36
 * @version：1.0
 **/
@Service
public class UserService extends BaseService<IUserMapper, User> implements IUserService {

    @Autowired
    private IUserRoleService userRoleService;

    @Override
    @Commit
    public void addOrUpdate(User user, String roleId) {
        if (CommonUtil.properIsNULL(user.getId())) {
            User temp = findByUsername(user.getUsername());
            if (CommonUtil.isEmpty(temp)) {
                String salt = CipherUtil.createSalt();
                String password = CipherUtil.createPwdEncrypt(user.getUsername(), user.getPassword(), salt);
                user.setSalt(salt);
                user.setPassword(password);
                super.insert(user);
                if (StringUtils.isNotEmpty(roleId)) {
                    List<UserRole> userRoles = Arrays.asList(roleId.split(",")).stream().map(c -> new UserRole(user.getId(), Long.valueOf(c))).collect(Collectors.toList());
                    userRoleService.insertBatch(userRoles, userRoles.size());
                }
            } else {
                throw new ApiException("[" + user.getUsername() + "]" + "已注册");
            }
        } else {
            User temp = super.selectById(user.getId());
            String passw = user.getPassword();
            //判断密码是否修改了
            if (StringUtils.isNotEmpty(passw) && !passw.equals(temp.getPassword())){
                passw = CipherUtil.createPwdEncrypt(user.getUsername(), CipherUtil.generatePassword(passw), temp.getSalt());
                user.setPassword(passw);
            }
            super.updateById(user);
            if (StringUtils.isNotEmpty(roleId)){
                UpdateWrapper wrapper = new UpdateWrapper();
                wrapper.eq("user_id", user.getId());
                userRoleService.delete(wrapper);
                List<UserRole> userRoles = Arrays.asList(roleId.split(",")).stream().map(c -> new UserRole(user.getId(), Long.valueOf(c))).collect(Collectors.toList());
                userRoleService.insertBatch(userRoles);
            }
        }
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        return super.selectOne(wrapper);
    }
}
